package com.cqjtu.tto.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cqjtu.tto.dao.mapper.ScreenshotRecordMapper;
import com.cqjtu.tto.dao.mapper.SysUserMapper;
import com.cqjtu.tto.dao.pojo.ScreenshotRecord;
import com.cqjtu.tto.dao.pojo.SysUser;
import com.cqjtu.tto.service.ScreenshotRecordService;
import com.cqjtu.tto.vo.EmployeeMonitorVo;
import com.cqjtu.tto.vo.ErrorCode;
import com.cqjtu.tto.vo.Results;
import com.cqjtu.tto.vo.ScreenshotRecordVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Base64;
import java.util.Comparator;
import java.util.List;

@Service
public class ScreenshotRecordServiceImpl extends ServiceImpl<ScreenshotRecordMapper, ScreenshotRecord> implements ScreenshotRecordService {

    @Autowired
    private SysUserMapper sysUserMapper;
    @Override
    public Results listMonitorRecord() {

        return null;
    }

    /**
     *
     * 获取员工的基本信息
     * @return
     */
    @Override
    public Results listEmployeeMonitor() {
        // 获取全部员工
        List<SysUser> sysUsers = sysUserMapper.selectList(new LambdaQueryWrapper<SysUser>().isNotNull(SysUser::getDepartmentId));
        List<EmployeeMonitorVo> employeeMonitorVos = new ArrayList<EmployeeMonitorVo>();
        if (sysUsers == null || sysUsers.size() ==0)
            return Results.fail(1011, "获取失败");
        sysUsers.forEach(sysUser -> {
            employeeMonitorVos.add(new EmployeeMonitorVo(sysUser.getUserId(), sysUser.getUserName(), sysUser.getIp()));
        });
        return Results.success(employeeMonitorVos);
    }

    /**
     * 根据员工id获取员工的历史监控记录
     * @param userId
     * @return
     */
    @Override
    public Results getEmployeeByUserId(Long userId) {
        // 获取用户信息
        SysUser user = sysUserMapper.selectOne(new LambdaQueryWrapper<SysUser>().eq(SysUser::getUserId, userId));
        if (user == null)
            return Results.fail(ErrorCode.PARAMS_ERROR.getCode(), ErrorCode.PARAMS_ERROR.getMsg());
        // 查询监控记录
        List<ScreenshotRecord> recordList = this.list(new LambdaQueryWrapper<ScreenshotRecord>()
                .eq(ScreenshotRecord::getUserId, userId));
        List<ScreenshotRecordVo> screenshotRecordVos = new ArrayList<>();
        if (recordList == null|| recordList.size() == 0)
            return Results.success(screenshotRecordVos);
        // 按时间倒叙
        Comparator<ScreenshotRecord> byTime = Comparator.comparing(ScreenshotRecord::getScreenshotTime).reversed();
        recordList.sort(byTime);
        Base64.Encoder encoder = Base64.getEncoder();
        recordList.forEach(screenshotRecord -> {
            Path path = Paths.get(screenshotRecord.getUrl());
            try {
                screenshotRecordVos.add(new ScreenshotRecordVo(screenshotRecord.getScreenshotId(),
                        user.getUserName(), screenshotRecord.getScreenshotTime(),
                        encoder.encodeToString(Files.readAllBytes(path))));
            } catch (IOException e) {
                e.printStackTrace();
            }
        });
        return Results.success(screenshotRecordVos);
    }
}
